package store.lunangangster.engine.bpmn.parser.handler;

import store.lunangangster.bpmn.model.BaseElement;
import store.lunangangster.bpmn.model.FlowElement;
import store.lunangangster.bpmn.model.Process;
import store.lunangangster.engine.bpmn.parser.BpmnParser;
import store.lunangangster.engine.impl.Context;
import store.lunangangster.engine.impl.entity.ProcessDefinitionEntity;
import store.lunangangster.engine.impl.entity.mgr.ProcessDefinitionEntityManager;

public class ProcessParseHandler extends AbstractBpmnParseHandler<Process> {

  @Override
  public Class<? extends BaseElement> getType() {
    return Process.class;
  }

  @Override
  public void handle0(BpmnParser bpmnParser, Process process) {
    bpmnParser.setCurrentProcess(process);
    String id = process.getId();
    String name = process.getName();
    String documentation = process.getDocumentation();
    ProcessDefinitionEntityManager processDefinitionEntityManager = Context.getCommandContext()
        .getProcessEngineConfiguration().getProcessDefinitionEntityManager();
    ProcessDefinitionEntity processDefinitionEntity = processDefinitionEntityManager.create();
    processDefinitionEntity.setKey(id); // Process's id <==> ProcessDefinitionEntity's key
    processDefinitionEntity.setName(name);
    processDefinitionEntity.setDocumentation(documentation);
    bpmnParser.getProcessDefinitionEntities().add(processDefinitionEntity);
    // using ParseHandlers on every sub FlowNode of this Process
    for (FlowElement flowElement : process.getFlowElements().values()) {
      bpmnParser.setCurrentElement(flowElement);
      bpmnParser.getBpmnParseHandlers().handleElement(bpmnParser, flowElement);
    }
  }

}
